package exam.db.dao;

import java.lang.reflect.Field;

public class StatementSql<T> {
	
	public String insertSql(Class<T> entityNames) {
		StringBuffer statement = new StringBuffer();
		StringBuffer values = new StringBuffer();
		statement.append("INSERT INTO ");
		values.append("VALUES(");
		try {
			T names = entityNames.newInstance();
			Field[] fields = entityNames.getDeclaredFields();
			// append tên bảng
			statement.append(fields[0].get(names)).append("(");
			// append tên các trường trong bảng ngoại trừ trường id
			for(int i = 2; i < fields.length - 1; i++) {
				statement.append(fields[i].get(names)).append(",");
				values.append("?").append(",");
			}
			statement.append(fields[fields.length - 1].get(names)).append(")");
			statement.append(values.append("?").append(")"));
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return statement.toString();
	}
}
